using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine;
using SCG.SolarVengeanceEngine.BrainInfo;

namespace SCG.SolarVengeance.Brains
{
    public class Beginner : Brain
    {
        public int NumCycles = 0;

        public override ChallengeLevel Challenge
        {
            get
            {
                return ChallengeLevel.Training;
            }
        }

        public override void Execute()
        {
            //Determine Cycle Number
            NumCycles++;

            //First determine how many StarSystem we own.
            int systemsOwned = 0;
            foreach (StarSystemInfo sys in StarSystems)
                if (sys.OwnedByMe)
                    systemsOwned++;


            //Count Drones, Warships, Emines Built
            int NumDrones = 0;
            int NumWarships = 0;
            int NumEmines = 0;
            double DroneRatio = 0;
            double EmineRatio = 0;
            foreach (StarShipInfo ship in MyStarShips)
                if (ship.OwnedByMe)
                {
                    if (ship.ShipType == StarShipType.Drone)
                        NumDrones++;
                    if (ship.ShipType == StarShipType.WarShip)
                        NumWarships++;
                    if (ship.ShipType == StarShipType.EMine)
                        NumEmines++;
                }
            if (systemsOwned > 0)
            {
                DroneRatio = NumDrones / systemsOwned;
                EmineRatio = NumEmines / systemsOwned;
            }

            //Cycle through StarSystems
            StarSystemInfo sys2 = null;
            int wpn = 0;
            int nbr = 1;
            int rsc = 0;
            int n = 1;
            foreach (StarSystemInfo sys in StarSystems)
                if (sys.OwnedByMe)
                {
                    //Special Processing for Capital StarSystem
                    if (sys.IsCapital)
                    {
                        if (sys.Shields < 15)
                        {
                            BuildShields(sys, 1);
                        }
                        else if ((sys.Resources > 9) & (EmineRatio < 2))
                        {
                            BuildStarShip(sys, StarShipType.EMine, 1, 0, false);
                        }
                        else if ((sys.Resources > 15) & (DroneRatio < 2))
                        {
                            BuildStarShip(sys, StarShipType.Drone, Random(4, 5), 0, false);
                        }
                        else if ((NumEmines == 2) & (NumDrones == 2) & (NumWarships == 0) & (NumCycles > 15) & (systemsOwned < 2))
                        {
                            BuildStarShip(sys, StarShipType.WarShip, 3, 5, false);
                        }
                    }
                    else  //Processing for other StarSystems
                    {
                        if (sys.Shields < sys.Value)
                        {
                            BuildShields(sys, 1);
                        }
                        else if (DroneRatio < 3)
                        {
                            BuildStarShip(sys, StarShipType.Drone, Random(4, 5), 0, false);
                        }
                        else if (EmineRatio < 3)
                        {
                            BuildStarShip(sys, StarShipType.EMine, 1, 0, false);
                        }
                        else
                        {
                            sys2 = ClosestStarSystem(sys.X, sys.Y, 20, FindSystemFlags.VisibleUnfriendly);
                            if (sys2 != null)
                            {
                                wpn = sys.Shields + 1;
                                rsc = sys.Value;
                                while (wpn > 7)
                                {
                                    wpn = wpn / 2;
                                    nbr++;
                                }
                                while (n <= nbr)
                                {
                                    n++;
                                    BuildStarShip(sys, StarShipType.WarShip, Random(5, 10), wpn, false);
                                }
                            }
                            else
                            {
                                BuildStarShip(sys, StarShipType.WarShip, Random(5, 10), Random(3, 8), false);
                            }
                        }
                    }
                }

            //Now process our StarShips
            StarShipInfo ship2 = null;
            foreach (StarShipInfo ship in MyStarShips)
                if (ship.OwnedByMe)
                {
                    if (ship.ShipType == StarShipType.WarShip)
                    {
                        sys2 = ClosestStarSystem(ship.X, ship.Y, 32000, FindSystemFlags.Unfriendly);
                        if (sys2 != null)
                        {
                            Target(ship, sys2);
                            AttackAdjacent(ship, false);
                        }
                    }
                    else if ((ship.ShipType == StarShipType.Drone) | (ship.ShipType == StarShipType.EMine))
                    {
                        ship2 = ClosestStarShip(ship.X, ship.Y, 100, FindShipFlags.Enemy, ShipKnownFlags.Either);
                        sys2 = ClosestStarSystem(ship.X, ship.Y, 32000, FindSystemFlags.Unfriendly);
                        if (ship2 != null)
                        {
                            Target(ship, ship2);
                        }
                        else if (sys2 != null)
                        {
                            Move(ship, sys2.X + Random(-10, 10), sys2.Y + Random(-10, 10));
                        }
                    }
                }

        }

        public override string Description
        {
            get
            {
                return "This is a super easy brain for Beginners.";
            }
        }
    }
}
